/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProjectMgtSystem.Action;

import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.util.*;

import ProjectMgtSystem.Entities.*;
import ProjectMgtSystem.Bean.*;

/**

 *
 * @author Su Duy Hien
 * Created date: 2011/07/19
 * Last Updated: 2011/07/21
 * Action class of project page

 * @author  Dang Duc Thinh
 * Last Update: 2011/07/27
 */
public class ProjectAction extends LookupDispatchAction {
    
// <editor-fold defaultstate="collapsed" desc="declare FORWARD_CONSTANTS">
    /* forward name="success" path="" */

    private final static String SUCCESS = "success";
    private final static String VIEW = "view";
    private final static String FAILURE = "failure";
    private final static String FIRST_UPDATE_ACTION = "viewSelectedID";
        private final static String VIEWTASKS = "viewTasks";
    // </editor-fold>

// <editor-fold defaultstate="collapsed" desc="mapping key">                
    /** Provides the mapping from resource key to method name.
     * @return Resource key / method name map.
     */
    @Override
    protected Map getKeyMethodMap() {
        Map map = new HashMap();

        map.put("ProjectAction.view", "view");
        map.put("ProjectAction.insert", "insert");
        map.put("ProjectAction.update", "update");
        map.put("ProjectAction.delete", "delete");
        map.put("ProjectAction.select", "select");
//++ThinhDD Last Update 2011/07/27
        map.put("ProjectAction.viewTasksAssigned", "viewTasksAssigned");
        map.put("ProjectAction.viewTasksNotAssigned", "viewTasksNotAssigned");
        map.put("ProjectAction.viewTasks", "viewTasks");
        map.put("ProjectAction.assignTasks", "assignTasks");
        map.put("ProjectAction.deleteTasks", "deleteTask");
//--ThinhDD
        return map;
    }
// </editor-fold>
    
// <editor-fold defaultstate="collapsed" desc="View, Update, Select, Delete, Insert action by HIENSD 26/07/2011">                
    /**
     * Load project's List
     * @param form
     * @return ArrayList<project>
     * @throws SQLException 
     */
    private ArrayList<Project> LoadListProject(ActionForm form) throws SQLException {
        ProjectActionBean PFrm = (ProjectActionBean) form;
        return PFrm.view();
    }

    /**
     * Load engineer's List
     * @param form
     * @return ArrayList<project>
     * @throws SQLException 
     */
    private ArrayList<Engineer> LoadListEngineer(ActionForm form) throws SQLException {
        EngineerActionBean eFormBean = (EngineerActionBean) form;
        return eFormBean.view();
    }

    /**
     * Action called view action
     */
    public ActionForward view(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {

        request.setAttribute("projectList", LoadListProject(form));
        request.setAttribute("engineerList", LoadListEngineer(new EngineerActionBean()));
        request.setAttribute("errorMessage", "");
        return mapping.findForward(VIEW); // Forward to admin/EngineerInfo.jsp
    }

    /** Action called on Add button click
     */
    public ActionForward insert(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        // TODO: implement add method
        ProjectActionBean prForm = (ProjectActionBean) form;
        Project p = new Project();
        p.setProjectID(prForm.getProjectID());
         
        p.setProjectName(prForm.getProjectName());
        Engineer en = new Engineer();
        en.setEngineerID(prForm.getTeamLeader());
        p.setTeamLeader(en);
        p.setDescription(prForm.getDescription());
	p.setPlanStartDate(prForm.getPlanStartDate());
        p.setPlanEndDate(prForm.getPlanEndDate());
        String message = prForm.insert(p);

        request.setAttribute("projectList", LoadListProject(form));
        request.setAttribute("engineerList", LoadListEngineer(new EngineerActionBean()));
        request.setAttribute("errorMessage", message);
        //request.setAttribute("errorMessage", "Insert_"+message);

        return mapping.findForward(VIEW);

    }

    /**
     * 
     */
    public ActionForward select(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {

        ProjectActionBean prFrm = (ProjectActionBean) form;
        request.setAttribute("prListUpdate", prFrm.select(prFrm.getProjectID()));
        request.setAttribute("engineerList", LoadListEngineer(new EngineerActionBean()));

        return mapping.findForward(FIRST_UPDATE_ACTION); // Forward to admin/UpdateProjectInfo.jsp
    }

    /** Action called on Edit button click
     */
    public ActionForward update(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws SQLException {
        ProjectActionBean prFrm = (ProjectActionBean) form;
        Project p = new Project();
        p.setProjectID(prFrm.getProjectID());
         
        p.setProjectName(prFrm.getProjectName());
        Engineer en = new Engineer();
        en.setEngineerID(prFrm.getTeamLeader());
        p.setTeamLeader(en);
        p.setDescription(prFrm.getDescription());
	p.setPlanStartDate(prFrm.getPlanStartDate());
        p.setPlanEndDate(prFrm.getPlanEndDate());
        String message = prFrm.update(p);
        request.setAttribute("errorMessage", message);
        request.setAttribute("projectList", LoadListProject(form));
        request.setAttribute("engineerList", LoadListEngineer(new EngineerActionBean()));
        request.setAttribute("errorMessage", message);

        return mapping.findForward(VIEW);

    }

    /** Action called on Delete button click
     */
    public ActionForward delete(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        ProjectActionBean prFrm = (ProjectActionBean) form;
        String ProjectID = prFrm.getProjectID();
        String message = prFrm.delete(ProjectID);

        request.setAttribute("projectList", LoadListProject(form));
        request.setAttribute("engineerList", LoadListEngineer(new EngineerActionBean()));
        request.setAttribute("errorMessage", message);

        return mapping.findForward(VIEW);

    }
// </editor-fold>   
    
// <editor-fold defaultstate="collapsed" desc="viewTasksAssigned   by ThinhDD CreateDate: 2011/07/25">        
    public ActionForward viewTasksAssigned(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        ProjectActionBean projectForm = (ProjectActionBean) form;
        String projectID = projectForm.getProjectID();
        
        ArrayList<Task> assignedTaskList = projectForm.viewTasksAssigned(projectID);
        
        projectForm.setAssignedTaskList(assignedTaskList);
        request.setAttribute("assignedTaskList", projectForm.getAssignedTaskList());
        return mapping.findForward(VIEWTASKS);
    }
// </editor-fold>  
    
// <editor-fold defaultstate="collapsed" desc="viewTasksNotAssigned   by ThinhDD CreateDate: 2011/07/25">        
    public ActionForward viewTasksNotAssigned(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        ProjectActionBean projectForm = (ProjectActionBean) form;
        String projectID = projectForm.getProjectID();
        
        ArrayList<Task> notAssignedTaskList = projectForm.viewTasksNotAssigned(projectID);
        
        projectForm.setNotAssignedTaskList(notAssignedTaskList);
        request.setAttribute("notAssignedTaskList", projectForm.getNotAssignedTaskList());
        return mapping.findForward(VIEWTASKS);
    }
// </editor-fold>     
    
// <editor-fold defaultstate="collapsed" desc="viewTasks   by ThinhDD CreateDate: 2011/07/25">        
    public ActionForward viewTasks(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        ProjectActionBean projectForm = (ProjectActionBean) form;
        String projectID = projectForm.getProjectID();
        
        ArrayList<Task> assignedTaskList = projectForm.viewTasksAssigned(projectID);
        ArrayList<Task> notAssignedTaskList = projectForm.viewTasksNotAssigned(projectID);
        
        projectForm.setAssignedTaskList(assignedTaskList);
        projectForm.setNotAssignedTaskList(notAssignedTaskList);
        request.setAttribute("assignedTaskList", projectForm.getAssignedTaskList());
        request.setAttribute("notAssignedTaskList", projectForm.getNotAssignedTaskList());
        
        return mapping.findForward(VIEWTASKS);
    }
// </editor-fold>         
    
// <editor-fold defaultstate="collapsed" desc="assignTasks   by ThinhDD CreateDate: 2011/07/25">        
    public ActionForward assignTasks(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String message = "-1";
        ProjectActionBean projectForm = (ProjectActionBean) form;
        String projectID = projectForm.getProjectID();
        String[] selectedItems = projectForm.getSelectedItems();
        
        for(String string : selectedItems){
            message = projectForm.assignTasks(projectID, string);
        }
        
        
        //
        ArrayList<Task> assignedTaskList = projectForm.viewTasksAssigned(projectID);
        ArrayList<Task> notAssignedTaskList = projectForm.viewTasksNotAssigned(projectID);
        projectForm.setAssignedTaskList(assignedTaskList);
        projectForm.setNotAssignedTaskList(notAssignedTaskList);
        request.setAttribute("assignedTaskList", projectForm.getAssignedTaskList());
        request.setAttribute("notAssignedTaskList", projectForm.getNotAssignedTaskList());  
        request.setAttribute("errorMessage", message);
        //
        
        return mapping.findForward(VIEWTASKS);
    }
// </editor-fold>    
    
// <editor-fold defaultstate="collapsed" desc="deleteTasks   by ThinhDD CreateDate: 2011/07/25">        
    public ActionForward deleteTask(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String message;
        ProjectActionBean projectForm = (ProjectActionBean) form;
        String projectID = projectForm.getProjectID();
        String taskID = projectForm.getTaskID();
//        
        message = projectForm.deleteTask(projectID, taskID);
        
        //
        ArrayList<Task> assignedTaskList = projectForm.viewTasksAssigned(projectID);
        ArrayList<Task> notAssignedTaskList = projectForm.viewTasksNotAssigned(projectID);
        projectForm.setAssignedTaskList(assignedTaskList);
        projectForm.setNotAssignedTaskList(notAssignedTaskList);
        request.setAttribute("assignedTaskList", projectForm.getAssignedTaskList());
        request.setAttribute("notAssignedTaskList", projectForm.getNotAssignedTaskList()); 
        request.setAttribute("errorMessage", message);
        //
        return mapping.findForward(VIEWTASKS);
    }
// </editor-fold>        
}
